package news.action;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import news.bean.News;
import news.factory.NewsFactory;
import news.service.NewsService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class NewsAction extends ActionSupport implements SessionAware{
	private News News=new News();
	private List<News> Newslist=new ArrayList<News>();
	private Map session;
	private File image;    //file to upload 
	private String imageFileName; //file name
	private String imageContentType;  //file type
	private InputStream inputStream;
	private String fileName;
	private int pagenow;
	
	public int getPagenow(){
		return pagenow;
	}
    public  void setPagenow(int pagenow){
    	this.pagenow=pagenow;
    }
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	 public File getImage() {
	        return image;
	    }

	    public void setImage(File img) {
	        this.image = img;
	    }

	    public String getImageFileName() {
	        return imageFileName;
	    }

	    public void setImageFileName(String imageFileName) {
	        this.imageFileName = imageFileName;
	    }

	    public String getImageContentType() {
	        return imageContentType;
	    }

	    public void setImageContentType(String imageContentType) {
	        this.imageContentType = imageContentType;
	    }


	public void setNews(News Newsbean){
		this.News=Newsbean;
	}
	
	public News getNews(){
		return News;
	}
	
	public List<News> getNewslist(){
		return Newslist;
	}
	
	public void setNewslist(List<News> Newslist){
		this.Newslist=Newslist;
	}
	
	
	
	public String modifyNews() throws Exception {
		if(News.getTitle().trim().equalsIgnoreCase("")){
			this.addActionError("modifyNews error");
			return INPUT;
		}
	   if(News.getContent().trim().equalsIgnoreCase("")){
		   this.addActionError("modifyNews error");
		   return INPUT;
	   }

		NewsService Newsservice=NewsFactory.getInstance().creatNewsService();
        if(!Newsservice.ModifyNews(News)){
        	this.addActionError("modifyNews error");
        	return INPUT;
        }
		
		Map session = ActionContext.getContext().getSession();
		session.remove("News");
		return SUCCESS;
	}
		public Map getSession() {
		return session;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	public String deleteNews() throws Exception {
		NewsService Newsservice =NewsFactory.getInstance().creatNewsService();
		if (!Newsservice.DeleteNews(News)) {
			this.addActionError("deleteNews error");
			return INPUT;
		} else {
			this.addActionError("deleteNews error");
			return SUCCESS;
		}
	}
	
	public String addNews() throws Exception {	
		if(image!=null){
		Random rnd = new Random();   // 随机数
		int p = rnd.nextInt(20);   // 范围是0`20
		String num = Integer.toString(p);   //定义一个num 将int转化成string
		String path=ServletActionContext.getServletContext().getRealPath("/");   //获取当前路径
		path=path.replace("bestcleaner", "images"); 
		imageFileName = num + "." + imageFileName;
		File file = new File(path + "//" + imageFileName);
		imageFileName="/images/"+imageFileName;
		FileUtils.copyFile(image, file);
		}
		else 
			imageFileName="";
		NewsService Newsservice=NewsFactory.getInstance().creatNewsService();
		System.out.println(News.getTitle());
		if (News.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("addNews error");
			return INPUT;
		}
		if (News.getContent().trim().equalsIgnoreCase("")) {
			this.addActionError("addNews error");
			return INPUT;
		}
		if(!Newsservice.AddNews(News,imageFileName)){
			this.addActionError("addNews error");
			return INPUT;
		}
		this.addActionError("addNews error");
		return SUCCESS;
	}

	 public String showallNews() throws Exception{
		 NewsService Newsservice=NewsFactory.getInstance().creatNewsService();
		 Newslist=Newsservice.ShowAllNews(getPagenow(),6);
		 if(getPagenow()==0){
			 int total=Newsservice.CountAllNews();
			 int page=0;
			 do {
					total = total - 6;
					page++;
				} while (total > 0);
				this.session.put("newstotalpage", page);
		 }
		 if(Newslist.size()==0){
			 this.addActionError("showallNews error");
			return INPUT;
		 }else{
			 return  SUCCESS; 
		 }
	 }
	 
	 public String showoneNews() throws Exception{
			NewsService NewsService =NewsFactory.getInstance().creatNewsService();
			News= NewsService.ShowoneNews(News);
			if (News == null) {
				this.addActionError("Error!");
				return INPUT;
			} else {
				this.session.put("News", News);
				return SUCCESS;
			}
		}
	 }
